<template>
  <card ref="Card" class="mb-15">
    <div slot="header">
      <span>录入单号</span>
      <submit-button ref="Submit" @submit="submit" text="录入" class="float-right"/>
    </div>
    <el-form :model="form" :rules="rules" ref="Form" inline hide-required-asterisk>
      <el-form-item prop="begin">
        <el-input v-model.trim="form.begin" placeholder="开始号码" clearable></el-input>
      </el-form-item>
      <el-form-item prop="end">
        <el-input v-model.trim="form.end" placeholder="结束号码" clearable></el-input>
      </el-form-item>
      <el-form-item class="float-right">
        <el-button class="el-icon-view" type='text' @click="check">已录入</el-button>
      </el-form-item>
    </el-form>
    <el-dialog title="已录入单号" width="99%" :visible.sync="visible">
      <div class="numberRange">
        <div v-for="item in entered" :key="item" class="mb-10">
          <span>{{item}}</span>
          <span>|</span>
        </div>
      </div>
    </el-dialog>
  </card>
</template>

<script>
  import {enteringApi, getEnteringApi} from '@/api/order/number'
  import {validateThirteenNumber} from "@/utils/validate";
  import {resetForm} from "@/utils/common";

  export default {
    name: "Enter",
    data() {
      return {
        max: 30000,
        visible: false,
        formData: [],
        form: {
          begin: '',
          end: ''
        },
        rules: {
          begin: [
            {required: true, message: '请输入开始号码', trigger: 'blur'},
            {validator: validateThirteenNumber, trigger: 'blur'}
          ],
          end: [
            {required: true, message: '请输入结束号码', trigger: 'blur'},
            {validator: validateThirteenNumber, trigger: 'blur'}
          ]
        }
      }
    },
    computed: {
      entered() { // 区间
        if (this.formData.length === 0) {
          return '无'
        } else {
          let list = [];
          this.formData.forEach(item => {
            if (item.begin === item.end) {
              list.push(this.add0(item.begin))
            } else {
              list.push(`${this.add0(item.begin)} ~ ${this.add0(item.end)}`)
            }
          });
          return list
        }
      }
    },
    methods: {
      add0(value) {
        value = value + '';
        while (value.length < 13) {
          value = '0' + value;
        }
        return value
      },
      check() {
        this.$refs.Card.start();
        getEnteringApi().then(result => {
          this.formData = result.resultParam.enteringList
          this.visible = true;
          this.$refs.Card.stop();
        })
      },
      submit() {
        this.$refs['Form'].validate(valid => {
          if (!valid) return false;
          let data = {...this.form};
          data.begin = data.begin / 1;
          data.end = data.end / 1;
          if (data.begin > data.end) {
            this.$errorMsg('结束号码不得小于开始号码');
            return;
          }
          if (data.end - data.begin > this.max) {
            this.$errorMsg(`一次录入不得超过 ${this.max} 条`);
            return;
          }
          this.$msgBox('确认录入？').then(() => {
            this.$refs.Submit.start();
            enteringApi(data).then(result => {
              this.$refs.Submit.stop();
              if (result.status === 200) this.cancel()
            }).catch(() => {
              this.$refs.Submit.stop();
            })
          })
        });
      },
      cancel() {
        resetForm(this)
      }
    }
  }
</script>
